#!/usr/local/bin/python3

__author__ = '曹植'

import json
import requests
from log import logger
from sign import login_md5_sign


def joint_login_function(channel_id, yyd_login_sql, uid, channel_addr):
    """
    联合登录
    :param channel_id:
    :param yyd_login_sql:
    :param uid:
    :param channel_addr:
    :return:
    """
    try:
        mobile = yyd_login_sql.query_mobile_by_uid(uid)
        result = requests.post(url=channel_addr, json=login_md5_sign(mobile)).json()
        if 'downloadUrl' in json.dumps(result):
            yyd_login_sql.save_joint_login_data(uid, int(channel_id))
            return {'code': 0, 'url': result['bizData']['downloadUrl']}
        else:
            return {'code': 3, 'status': '无链接地址'}
    except Exception as e:
        logger.exception('优逸贷联合登录出错,{}'.format(e))
        return {'code': 1, 'status': '失败'}
    finally:
        yyd_login_sql.close()
